<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkUsername = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入您的账号!'))
  }

  if (!/[0-9a-zA-Z]$/.test(value)) {
    callback(new Error('账号只能是数字或字母!'))
  } else if (!/^(?=.*[0-9])(?=.*[a-zA-Z]).{8,16}$/.test(value)) {
    callback(new Error('账号必须包含字母、数字,长度为8-16!'))
  } else {
    callback()
  }
}

/* const validateCode = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入验证码!'))
  } else {
    if (ruleForm.code !== '') {
      callback(new Error("后端判断验证码是否正确"))
    }
    callback()
  }
} */
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码!'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  username: '',
  password: '',
  //code: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{ validator: checkUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
  //code: [{ validator: validateCode, trigger: 'blur' }],

})
import { login } from '@/apis/login'
import router from '@/router';
import { ElMessage } from 'element-plus'
import { useCounterStore } from '@/stores/counter';
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log('submit!')
      //发起请求 登录
      const res = await login(ruleForm)
      if (res.data.code === 1) {
        console.log('登录成功')
        //弹出一个轻提示
        ElMessage({
          message: '登录成功',
          type: 'success',
        })
        //存入用户信息
        console.log(res.data.data)
        useCounterStore().setUserInfo(res.data.data)
        console.log(useCounterStore().token, useCounterStore().name, useCounterStore().username, useCounterStore().permissions)
        //跳转页面
        router.push('/')
      } else {
        console.log('登录失败', res.data.msg)
        //弹出一个轻提示
        ElMessage.error(res.data.msg)
      }
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
} 
</script>

<template>
  <div class="background-image-example">
    <span style="color: black;;font-size: 40px;">药品商城后台管理系统</span>
    <div style="margin-top: 10%;display: flex;justify-content: center;align-items: center;">
      <el-card style="width: 500px;height: 40vh;">
        <template #header>
          <div style="text-align: center;">
            <h1>登录</h1>
          </div>
        </template>
        <div style="margin-right: 10%;">
          <el-form ref="ruleFormRef" size="large" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">
            <el-form-item label="账号" prop="username">
              <el-input v-model.string="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
            </el-form-item>

            <!-- <el-form-item label="验证码" prop="code">
      <el-input
        v-model="ruleForm.code"
      />
    </el-form-item> -->

            <el-form-item style="margin-left: 10%;">
              <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
              <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.background-image-example {
  background-image: url('../assets/1.jpg');
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  text-align: center;
}
</style>
